package org.nocepko.android.sumc.task;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;
import android.util.Log;

public class DownloadTask extends AsyncTask<String, Integer, String> {

	private final static String LOG_TAG = "sumc.task.DownloadTask";
	
	@Override
	protected String doInBackground(String... params) {

		HttpRequestBase getMethod;
		HttpClient client = new DefaultHttpClient();
		
		try {
			getMethod = new HttpGet(params[0]);
		} catch (IllegalArgumentException e) {
			Log.w(LOG_TAG, "Looks like an invalid url that thing ...");
			return null;
		}

		HttpResponse response;
		String result = null;
		try {
			response = client.execute(getMethod);
			HttpEntity entity = response.getEntity();
			result = EntityUtils.toString(entity);
		} catch (IOException e) {
			Log.w(LOG_TAG, e);
		} catch (ParseException e) {
			Log.w(LOG_TAG, e);
		} finally {
			client.getConnectionManager().shutdown();
		}
		
		return result;
	}

	@Override
	protected void onPostExecute(String result) {
		Log.i(LOG_TAG, "Got it!");
		Log.i(LOG_TAG + " source\n", result);
//		get
	}
}

